package com.lh.servletMain;/**
 * @package com.lh.servletMain
 * @author 刘辉
 * @date 2020/8/3 10:33
 * @param $
 */

import com.lh.interfacesss.HttpServlet;
import com.lh.interfacesss.WerkServlet;
import com.lh.test.TestMyServlet;

import javax.jws.WebService;
import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.*;

/**
 * @ClassName ServletLearn
 * @Description TODO
 * @Author 14784
 * @Date 2020/8/3 10:33
 * @Version 1.0
 */
public class ServletLearn {
    public static void main(String[] args) {

        exec("/test", HttpMethod.Get);
    }


    /**
     * 扫描位置的方法
     * @param path 请求的路径
     * @param method 请求的方法
     */
    public static void exec(String path,HttpMethod method){
        ClassLoader classLoader = ServletLearn.class.getClassLoader();
        //扫面当前目录下的所有文件
        String path1 = classLoader.getResource("").getPath();
        //扫面当前目录下的所有文件
        folderMethod1(path1,path, method);

    }

    /**
     * 扫描位置的方法
     * @param path
     * @return
     */
    public static void folderMethod1(String path,String oldPath,HttpMethod method) {
        File file = new File(path);
        LinkedList<File> list = new LinkedList<>();
        if (file.exists()) {
            if (null == file.listFiles()) {
                return ;
            }
            list.addAll(Arrays.asList(file.listFiles()));
            while (!list.isEmpty()) {
                File[] files = list.removeFirst().listFiles();
                if (null == files) {
                    continue;
                }
                for (File f : files) {
                    if (f.isDirectory()) {
                        list.add(f);
                    } else {
                        String string = f.toString();
                        String  folder= f.getParentFile().getParentFile().getParentFile().getParentFile().toString();
                        String tempClassPath = string.replace(folder,"").substring(1).replaceAll("\\\\", ".").replace(".class", "");
                        test(tempClassPath,oldPath,method);
                    }
                }
            }
        } else {
            System.out.println("文件不存在!");
        }
    }


    public  static void test(String tempClassPath,String oldPath,HttpMethod method){
        try {
            Class<?>  aClass =ClassLoader.getSystemClassLoader().loadClass(tempClassPath);
            WerkServlet annotation = aClass.getAnnotation(WerkServlet.class);
            if(annotation!=null){
                if (oldPath.equals(annotation.value())){
                    System.out.println("是test");
                }else{
                    System.out.println(annotation.value()+"不是需要的类");
                }
                HttpServlet t = null;
                Object o = aClass.newInstance();
                if (o instanceof HttpServlet){
                    t = (HttpServlet) o;
                }
                if (t ==null){
                    return;
                }
                if (method ==HttpMethod.Get){
                    t.doGet();
                }else if (method ==HttpMethod.Post){
                    t.doPost();
                }

            }else{
                System.out.println(tempClassPath+"方法中没有注解");
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }





}
